package com.syt.cellphone.information.ui

import NumberFormatUtil
import android.content.Intent
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ImageSpan
import android.util.Log
import com.syt.cellphone.information.App
import com.syt.cellphone.information.R
import com.syt.cellphone.information.base.BaseActivity
import com.syt.cellphone.information.http.RequestHelp
import com.syt.cellphone.information.http.doFailure
import com.syt.cellphone.information.http.doSuccess
import com.syt.cellphone.information.ui.main.MainActivity
import kotlinx.android.synthetic.main.activity_launcher.*
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 *  @author: syt
 *  创建时间: 2020/8/18 10:10
 *  @功能   应用启动活动
 *  @version v1.0
 */
open class LauncherActivity : BaseActivity(R.layout.activity_launcher) {

    val TAG = this.javaClass.simpleName

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
        } else {
            init()
        }

    }

    fun init() {
        launch {
            // 网络请求示例
            Log.d(TAG, "init: ${NumberFormatUtil.longToString(System.currentTimeMillis())} + ${Thread.currentThread().name}")
            RequestHelp.getBaseListPhone(0)
                .catch {
                    Log.d(TAG, "init: $it")
                }
                .collectLatest {value ->
                    value.doSuccess {
                        // 启动页面前，去加载一次界面
                        App.db.phoneBaseDao().deleteData()
                        App.db.phoneBaseDao().insert(
                            *it.list.toTypedArray()
                        )
                        launcher_view.postDelayed({ startMainActivity() }, 500)
                    }
                    value.doFailure {
                        toast(it.toString())
                    }
                }
        }
    }

    private fun startMainActivity() {
        tv_test.hint = getHint()
        startActivity<MainActivity>()
        finish()
    }

    private fun getHint(): CharSequence {
        // 测试主动添加图标
        val ssb = SpannableStringBuilder(" ")
        val drawable = getDrawable(R.drawable.ic_search_hint)!!
        drawable.setBounds(0, 0, 100, 100)
        ssb.setSpan(ImageSpan(drawable),0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        ssb.append("1231231")
        return ssb
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause: ")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop: ")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "onRestart: ")
    }
}

class Launcher1 : LauncherActivity()
class Launcher2 : LauncherActivity()
class Launcher : LauncherActivity()